package org.file5.controller.admin;

import org.apache.log4j.Logger;
import org.file5.domain.Person;
import org.file5.domain.BaseEntity;
import org.file5.domain.Person;
import org.file5.search.SearchUtility;
import org.file5.service.ServiceFascade;
import org.file5.util.DataBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.appengine.api.users.UserService;

@Controller
@RequestMapping("/admin/buddy")
public class AdminBuddyController {
	private final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private ServiceFascade sf;
	@Autowired
	private UserService googleUserService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String buddyList(
			@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			Model model) {

		if (pageNo == null || pageNo < 0) {
			pageNo = 0;
		}
		if (pageSize == null || pageSize < 1) {
			pageSize = DataBean.PAGE_SIZE_ADMIN;
		}
		DataBean<Person> dbc = sf.getPersonService().getPersons( pageNo * pageSize, pageSize);
		if (dbc.getData() != null && !dbc.getData().isEmpty()) {
			model.addAttribute("buddies", dbc.getData());
			model.addAttribute("totalItems", dbc.getCount());
			model.addAttribute("pageNo", pageNo);
			model.addAttribute("pageSize", pageSize);
		}		



		return "admin/buddy/buddies";
	}

	@RequestMapping("/publish")
	public String buddyPublish(@RequestParam(value = "uid") String uid,
			Model model) {
		Person buddy = sf.getPersonService().updateStatus(uid,
				BaseEntity.STATUS.ONLINE); 
		return "redirect:/admin/buddy/list.asp"  ;
	}

	@RequestMapping("/unpublish")
	public String buddyUnpublish(@RequestParam(value = "uid") String uid,
			Model model) {
		Person buddy = sf.getPersonService().updateStatus(uid,
				BaseEntity.STATUS.OFFLINE);

		return "redirect:/admin/buddy/list.asp"  ;
	}





}
